<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" inline>
      <el-form-item label="柜机名称" prop="cabinetId">
        <el-select
          v-model="queryParams.cabinetId"
          placeholder="请选择柜机名称"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="item in cabinetList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="组名/层级/货道" prop="name" label-width="110px">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入组名/层级/货道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="序号" type="index" width="80" align="center" />
      <el-table-column label="柜机名称" align="center">
        <template slot-scope="scope">
          {{ getCabinetLabel(scope.row.cabinetId) }}
        </template>
      </el-table-column>
      <el-table-column label="组名" align="center" width="100">
        <template slot-scope="scope"> {{ getGroupName(scope.row.name) }} </template>
      </el-table-column>
      <el-table-column label="层级" align="center" width="100">
        <template slot-scope="scope"> {{ getLevelName(scope.row.name) }} </template>
      </el-table-column>
      <el-table-column label="货道" align="center" prop="name" width="100" />
      <el-table-column label="药品名称" align="center">
        <template slot-scope="{ row }">
          <el-button v-if="row.medicineId" type="text" @click="handleView(row)">
            {{ row.medicineVo.medicineName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="库存" align="center" prop="inventory" width="120" />
      <el-table-column label="预警数量" align="center" prop="threshold" width="100" />
      <el-table-column label="库存状态" align="center" prop="status" width="100">
        <template slot-scope="{ row }">
          <el-tag
            v-if="row.medicineId"
            :type="
              row.inventory - row.threshold >= 0
                ? 'success'
                : row.inventory == 0
                ? 'danger'
                : 'warning'
            "
          >
            {{
              row.inventory - row.threshold >= 0
                ? "正常"
                : row.inventory == 0
                ? "售罄"
                : "紧急"
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="货道锁定" align="center" prop="locked">
        <template slot-scope="scope">
          <el-button
            :type="scope.row.locked == 0 ? 'success' : 'info'"
            size="mini"
            plain
            :icon="scope.row.locked == 0 ? 'el-icon-unlock' : 'el-icon-lock'"
            @click="handleStatusChange(scope.row)"
          >
            {{ scope.row.locked == 0 ? "未锁定" : "已锁定" }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="药品信息" :visible.sync="medicineShow" width="600">
      <el-form :model="medicineInfo" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="药品名称" prop="medicineName">
              {{ medicineInfo.medicineName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品规格" prop="medicineSpecifications">
              {{ medicineInfo.medicineSpecifications }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="条码" prop="barCode">
              {{ medicineInfo.barCode }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <dict-tag
                :options="dict.type.mc_medicine_unit"
                :value="medicineInfo.unit"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保质期" prop="qualityGuaranteePeriod">
              {{ medicineInfo.qualityGuaranteePeriod || "-" }}月
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="剂型" prop="dosageForm">
              <dict-tag
                :options="dict.type.mc_dosage_form"
                :value="medicineInfo.dosageForm"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产厂家" prop="manufacturer">
              {{ medicineInfo.manufacturer }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处方分类" prop="prescriptionType">
              <dict-tag
                :options="dict.type.mc_prescription_type"
                :value="medicineInfo.prescriptionType"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品种" prop="variety">
              <dict-tag
                :options="dict.type.mc_medicine_variety_type"
                :value="medicineInfo.variety"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="包装规格" prop="packagingSpecifications">
              {{ medicineInfo.packagingSpecifications }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产地" prop="origin">
              {{
                medicineInfo.origin == 1
                  ? "国产"
                  : medicineInfo.origin == 2
                  ? "进口"
                  : medicineInfo.origin == 0
                  ? "未知"
                  : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="新特药" prop="newSpecial">
              {{
                medicineInfo.newSpecial == 1
                  ? "是"
                  : medicineInfo.newSpecial == 0
                  ? "否"
                  : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="基药" prop="baseDrug">
              {{
                medicineInfo.baseDrug == 1 ? "是" : medicineInfo.baseDrug == 0 ? "否" : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="社保" prop="socialSecurity">
              {{
                medicineInfo.socialSecurity == 1
                  ? "是"
                  : medicineInfo.socialSecurity == 0
                  ? "否"
                  : ""
              }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="停产" prop="stopProduction">
              {{
                medicineInfo.stopProduction == 1
                  ? "是"
                  : medicineInfo.stopProduction == 0
                  ? "否"
                  : ""
              }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listCabinet } from "@/api/mc/cabinet";
import { listSlot, updateSlot } from "@/api/mc/slot";

export default {
  name: "mc-slot",
  dicts: [
    "mc_medicine_type",
    "mc_prescription_type",
    "mc_medicine_unit",
    "mc_dosage_form",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 货道信息表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cabinetId: null,
        name: null,
      },
      cabinetList: [], // 柜机列表
      medicineShow: false, // 药品信息弹窗
      medicineInfo: {}, // 药品信息
    };
  },
  created() {
    this.getCabinetList();
    this.getList();
  },
  methods: {
    /** 查询货道信息列表 */
    getList() {
      this.loading = true;
      listSlot(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 查询柜机列表 */
    getCabinetList() {
      listCabinet({ pageNum: 1, pageSize: 10000 }).then((res) => {
        this.cabinetList = res.rows;
      });
    },
    // 柜机中文
    getCabinetLabel(id) {
      let row = this.cabinetList.find((item) => item.id == id);
      return row ? row.name : "";
    },
    // 获取组名
    getGroupName(name = "") {
      if (!name) return "";
      return name.slice(0, 1);
    },
    // 获取层级
    getLevelName(name = "") {
      if (!name) return "";
      let list = name.split("-");
      return list.length ? list[0] : "";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 查看药品信息
    handleView(row) {
      this.medicineInfo = row.medicineVo;
      this.medicineShow = true;
    },
    // 切换锁定状态
    handleStatusChange(row) {
      let text = row.locked == 0 ? "锁定" : "解锁";
      this.$modal
        .confirm(`确认要${text}该货道吗?`)
        .then(() => {
          updateSlot({
            id: row.id,
            locked: row.locked == 0 ? 1 : 0,
          }).then(() => {
            this.$modal.msgSuccess(`${text}成功`);
            this.getList();
          });
        })
        .catch(() => {});
    },
  },
};
</script>
